/**
 * 
 */
package com.oet.service.mail;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.MailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

/**
 * @author y140zhan
 *
 */
public class SimpleEmailService implements EmailServiceInterface {

	protected final Log logger = LogFactory.getLog(getClass());

    private JavaMailSenderImpl mailSender;
	
    private String mailHost;
    private String userName;
    private String password;
    private String mailPort;
    private boolean useSSL;
    
    private String subjectPrefix = null;
    private String subjectSuffix = null;
    
    
    public void setMailSender(JavaMailSenderImpl mailSender)
    {
    	this.mailSender = mailSender;
    }
    
    public void setSubjectSuffix(String subjectSuffix)
    {
    	this.subjectSuffix = subjectSuffix;
    }
    public void setSubjectPrefix(String subjectPrefix)
    {
    	this.subjectPrefix = subjectPrefix;
    }
    
    public MailInfo getMailInfo() 
	{
		// TODO Auto-generated method stub
		return new MailInfo();
	}

	public MailInfo getMailInfo(String to, String subject,Boolean importance) 
	{
		// TODO Auto-generated method stub
		return new MailInfo( to, subject, importance);
	}


	public Boolean send(MailInfo mail) throws MessagingException{
		// TODO Auto-generated method stub
		if (mail.isValid())
		{
			MimeMessage mimeMessage = mailSender.createMimeMessage();
			MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
			message.setTo((String[])(mail.getSendTo().toArray(
					new String[mail.getSendTo().size()])));
			
			if(!mail.getCc().isEmpty())
			{
				message.setCc((String[])(mail.getCc().toArray(
						new String[mail.getCc().size()])));
			}
			if(!mail.getBcc().isEmpty())
			{
				message.setBcc((String[])(mail.getBcc().toArray(
						new String[mail.getBcc().size()])));
			}
			if(!mail.getAttachments().isEmpty())
			{
				;
			}
			try {
				message.setFrom(mail.getFromAddress(), mail.getFromName());
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			message.setSubject(mail.getSubject() + this.subjectSuffix);
			message.setText(mail.getContent(), true);

		   // message.addInline("myLogo", new ClassPathResource("ydk_logo.gif"));
		    
		    logger.debug("send mail to: " + mail.getSendTo());
		    logger.debug("send mail Subject: " + mail.getSubject());
		    mailSender.send(message.getMimeMessage());
		    
		    return true;
		}
		logger.warn("send invalid mail");
		return false;
	}


}
